package com.naiterui.ehp.bs.im.repository.mongo;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

/**
 * 与具体ORM实现无关的分页参数及查询结果封装.
 * <P>File name : Page.java </P>
 * <P>Author : zhouyanxin </P>
 * <P>Date : 2014-9-18 </P>
 */
public class Page<T> implements Serializable {

    // 公共变量
    public static final String ASC = "asc";
    public static final String DESC = "desc";

    public static final int MIN_PAGESIZE = 20;
    public static final int MAX_PAGESIZE = 5000;

    /**
     * 字段或域定义：<code>serialVersionUID</code>
     */
    private static final long serialVersionUID = -7618231474413524175L;

    // 分页参数
    protected int pageNo = 1;
    protected int pageSize = MIN_PAGESIZE;
    protected String orderBy;
    protected String order;

    // 返回结果
    protected List<T> result = Collections.emptyList();
    protected int totalCount = -1;

    // 构造函数
    public Page() {
        super();
    }

    public Page(final int pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * 获得当前页的页号,序号从1开始,默认为1.
     */
    public int getPageNo() {
        return this.pageNo;
    }

    /**
     * 设置当前页的页号,序号从1开始,低于1时自动调整为1.
     */
    public void setPageNo(final int pageNo) {
        this.pageNo = pageNo;
        if (pageNo < 1) {
            this.pageNo = 1;
        }
    }

    /**
     * 获得每页的记录数量,默认为10.
     */
    public int getPageSize() {
        return this.pageSize;
    }

    /**
     * 设置每页的记录数量,超出MIN_PAGESIZE与MAX_PAGESIZE范围时会自动调整.
     */
    public void setPageSize(final int pageSize) {
        this.pageSize = pageSize;
        if (pageSize > MAX_PAGESIZE) {
            this.pageSize = MAX_PAGESIZE;
        }
    }

    /**
     * 根据pageNo和pageSize计算当前页第一条记录在总结果集中的位置,序号从0开始.
     */
    public int getFirst() {
        return (this.pageNo - 1) * this.pageSize;
    }

    /**
     * 获得排序字段,无默认值.多个排序字段时用','分隔,仅在Criterion查询时有效.
     */
    public String getOrderBy() {
        return this.orderBy;
    }

    /**
     * 设置排序字段.多个排序字段时用','分隔.仅在Criterion查询时有效.
     */
    public void setOrderBy(final String orderBy) {
        this.orderBy = orderBy;
    }

    /**
     * 是否已设置排序字段,仅在Criterion查询时有效.
     */
    public boolean isOrderBySetted() {
        return StringUtils.isNotBlank(this.orderBy);
    }

    /**
     * 获得排序方向,默认为asc,仅在Criterion查询时有效.
     * 可选值为desc或asc,多个排序字段时用','分隔.
     */
    public String getOrder() {
        return this.order;
    }

    /**
     * 取得页内的记录列表.
     */
    public List<T> getResult() {
        return this.result;
    }

    public void setResult(final List<T> result) {
        this.result = result;
    }

    /**
     * 取得总记录数,默认值为-1.
     */
    public int getTotalCount() {
        return this.totalCount;
    }

    public void setTotalCount(final int totalCount) {
        this.totalCount = totalCount;
    }

    /**
     * 根据pageSize与totalCount计算总页数,默认值为-1.
     */
    public int getTotalPages() {
        if (this.totalCount < 0) {
            return -1;
        }

        int count = this.totalCount / this.pageSize;
        if (this.totalCount % this.pageSize > 0) {
            count++;
        }
        return count;
    }

    /**
     * 是否还有下一页.
     */
    public boolean isHasNext() {
        return this.pageNo + 1 <= getTotalPages();
    }

    /**
     * 取得下页的页号,序号从1开始.
     */
    public int getNextPage() {
        if (isHasNext()) {
            return this.pageNo + 1;
        } else {
            return this.pageNo;
        }
    }

    /**
     * 是否还有上一页.
     */
    public boolean isHasPre() {
        return this.pageNo - 1 >= 1;
    }

    /**
     * 取得上页的页号,序号从1开始.
     */
    public int getPrePage() {
        if (isHasPre()) {
            return this.pageNo - 1;
        } else {
            return this.pageNo;
        }
    }

    /**
     * 计算分页起始元素位置
     */
    public static int getStartOfPage(int pageNo, int pageSize) {
        return (pageNo - 1) * pageSize;
    }

}
